package com.huihuiyun.huihuiyun.server.service;

import com.huihuiyun.huihuiyun.server.domain.HhyMemberFavourite;
import com.huihuiyun.huihuiyun.server.repository.HhyMemberFavouriteRepository;
import com.mark.util.CommonUtil;
import com.mark.util.JaxrsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

@Component
@Path("/hhyMemberFavourites")
@Slf4j
public class HhyMemberFavouriteService {
    @Autowired
    private HhyMemberFavouriteRepository hhyMemberFavouriteRepository;
    @Context
    private HttpServletResponse httpServletResponse;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public HhyMemberFavourite post(HhyMemberFavourite hhyMemberFavourite) {
        try {
            hhyMemberFavourite.id = CommonUtil.createRandomId();
            hhyMemberFavourite = hhyMemberFavouriteRepository.save(hhyMemberFavourite);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.CREATED.getStatusCode());
            return hhyMemberFavourite;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @DELETE
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public void delete(@PathParam("id") String id) {
        try {
            hhyMemberFavouriteRepository.delete(id);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.NO_CONTENT.getStatusCode());
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public HhyMemberFavourite put(HhyMemberFavourite hhyMemberFavourite) {
        try {
            if (CommonUtil.isEmpty(hhyMemberFavourite.id)) {
                hhyMemberFavourite.id = CommonUtil.createRandomId();
            }
            hhyMemberFavourite = hhyMemberFavouriteRepository.save(hhyMemberFavourite);
            return hhyMemberFavourite;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public HhyMemberFavourite findOne(@PathParam("id") String id) {
        try {
            HhyMemberFavourite hhyMemberFavourite = hhyMemberFavouriteRepository.findOne(id);
            if (hhyMemberFavourite == null) {
                JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.NO_CONTENT.getStatusCode());
                return null;
            } else {
                return hhyMemberFavourite;
            }
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<HhyMemberFavourite> findAll() {
        try {
            List<HhyMemberFavourite> hhyMemberFavouriteList = hhyMemberFavouriteRepository.findAll();
            return hhyMemberFavouriteList;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findAll")
    public Page<HhyMemberFavourite> findAll(@QueryParam("pageNumber") int pageNumber, @QueryParam("pageSize") int pageSize) {
        try {
            PageRequest pageRequest = new PageRequest(pageNumber, pageSize);
            Page<HhyMemberFavourite> page = hhyMemberFavouriteRepository.findAll(pageRequest);
            return page;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findAllByMemberId")
    public Page<HhyMemberFavourite> findAllByMemberId(@QueryParam("memberId") String memberId, @QueryParam("pageNumber") int pageNumber, @QueryParam("pageSize") int pageSize) {
        try {
            PageRequest pageRequest = new PageRequest(pageNumber, pageSize);
            Page<HhyMemberFavourite> page = hhyMemberFavouriteRepository.findAllByMemberId(memberId, pageRequest);
            return page;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findListByMemberId")
    public List<HhyMemberFavourite> findListByMemberId(@QueryParam("memberId") String memberId) {
        try {
            List<HhyMemberFavourite> list = hhyMemberFavouriteRepository.findAllByMemberId(memberId);
            return list;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/branch")
    public void post(List<HhyMemberFavourite> hhyMemberFavouriteList) {
        try {
            hhyMemberFavouriteRepository.save(hhyMemberFavouriteList);
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }
}
